package org.frog.sgs.client.process.all;

import java.util.Random;

import org.apache.mina.core.session.IoSession;
import org.frog.sgs.card.general.General;
import org.frog.sgs.card.strength.StrengthFactory;
import org.frog.sgs.client.game.ClientInstance;
import org.frog.sgs.client.msg.ClientMsgChooseGeneralOver;
import org.frog.sgs.client.process.ClientProcessor;
import org.frog.sgs.data.GameDataManager;
import org.frog.sgs.io.SgsMsg;

/**
 * @author duanxiangqi
 * @date 2012-10-26
 */
public class ClientProcessorChooseGeneral implements ClientProcessor {

	public void process(IoSession session, SgsMsg msg, ClientInstance instance) throws Exception {
		int size = msg.readInt();
		int[] ids = new int[size];
		for (int i = 0; i < size; i++) {
			int id = msg.readInt();
			String name = msg.readString();
			ids[i] = id;
			System.out.print(name + "(" + id + ")；");
		}

		System.out.println("\n请输入您要选择的将领ID");
		// TODO int id = SgsCmdUtil.instance.readInt(ids);
		int id = ids[new Random().nextInt(ids.length)];
		General general = GameDataManager.instance.getGeneralCopy(id);
		instance.setGeneral(general);
		int maxStrength = general.getMaxStrength();
		if (instance.getRole().isZhugong()) {
			maxStrength++;
		}
		instance.setStrength(StrengthFactory.instance.newInstance(maxStrength));
		instance.sendMsg(new ClientMsgChooseGeneralOver(id));
	}
}
